<?php
/* ******************************
 *	Recipie Production List aJax actions
 * ******************************
 */
 
include('../../../includes/classes/core.php');
include('../../../includes/classes/log.class.php');

$log 		= new log();

$action = $_REQUEST['act'];
$error = '';
$data = '';

switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
        break;
    case 'get_edit_page':
	    $prod_id	= $_REQUEST['id'];
		$page		= GetPage(GetProduction($prod_id));
		
		$data		= array('page'	=> $page);

        break;
    case 'get_list':
	    $count		= $_REQUEST['count'];
	    $hidden		= $_REQUEST['hidden'];
	    $local_id	= $_REQUEST['local_id'];
	    
	    
	    $rResult = mysql_query("SELECT    	DISTINCT `recipie`.`id`,
											`production`.`name`,
											`recipie`.`quantity`,
											ROUND(reserve.price * recipie.quantity  * recipie.default_amount, 4),
											`recipie`.`price`
								FROM		`recipie`
								LEFT JOIN 	`production` ON `recipie`.`ingredient_id` = `production`.`id`
								LEFT JOIN 	reserve ON recipie.ingredient_id = reserve.production_id
								WHERE 		`recipie`.`recipie_id` = $local_id");
	    
		$data = array(
			"aaData"	=> array()
		);

		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
		
        break;
    case 'save_prod':
		$comp_id		= $_REQUEST['id'];
		$local_id		= $_REQUEST['l_id'];
		$amount         = $_REQUEST['am'];	
		$quantity  		= $_REQUEST['qn'];
		$price 			= $_REQUEST['pr'];
		$ing_name 		= $_REQUEST['rn'];
		$checked 		= $_REQUEST['ch'];
		$user_id		= $_COOKIE['USERID'];
		
		
		
		$ingredient_id		= GetProductID($ing_name);
		
		if($comp_id == ''){
			if(!CheckProductionExist($local_id, $ingredient_id)){
				AddProduction($local_id, $user_id, $ingredient_id, $amount, $quantity, $price, $checked);
				
				GLOBAL $log;
				$log->setInsertLog('recipie');
			}else{
				$error = ' უკვე არის სიაში!';
			}
		}else{
			SaveProduction($comp_id, $user_id, $ingredient_id, $amount, $quantity, $price, $checked);
		}

        break;
    case 'disable':
		$ingr_id = $_REQUEST['id'];
		DeleteProduction($ingr_id);
		
        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Recipie Production List Functions
 * ******************************
 */

function getProdId($ingredient_name){
	mysql_query("SELECT `production`.`id`
				FROM `production`
				WHERE `production`.`name` = '$ingredient_name'");
}


function AddProduction($recipie_id, $user_id, $ingredient_id, $amount, $quantity, $price, $checked)
{
	mysql_query("INSERT INTO recipie (recipie_id, user_id, ingredient_id, default_amount, quantity, price, visible)
  					VALUES   ($recipie_id, $user_id, $ingredient_id, $amount, $quantity, $price, $checked)");
}

function SaveProduction($comp_id, $user_id, $ingredient_id, $amount, $quantity, $price, $checked) 
{
	GLOBAL $log;
	$log->setUpdateLogBefore('recipie', $comp_id);
	
	mysql_query("UPDATE
	    			`recipie`
				 SET
					`user_id`		= $user_id,
					`ingredient_id`	= $ingredient_id,
					`default_amount`= $amount,
					`quantity`		= $quantity,
					`price`			= $price,
					`visible`		= $checked
				 WHERE
					`id` = $comp_id");
	
	$log->setUpdateLogAfter('recipie', $comp_id);
}

function DeleteProduction($prod_id)
{
    mysql_query("DELETE 
				 FROM
	    			`recipie`
				 WHERE
					`id` = $prod_id");

}

function CheckProductionExist($local_id, $ingredient_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT `recipie_id`
										  FROM `recipie`
										  WHERE `recipie_id` = $local_id && `ingredient_id` = $ingredient_id"));
	if($res['recipie_id'] != ''){
		return true;
	}
	return false;
}

function GetProductID($prod_name) 
{
	$prod_name = htmlspecialchars($prod_name, ENT_QUOTES);
    $res = mysql_fetch_assoc(mysql_query("SELECT `production`.`id`
													FROM `production`
													WHERE `production`.`name` = '$prod_name'"));
	return $res['id'];
}

function GetProduction($prod_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT    		`recipie`.`id`,
															`recipie`.`quantity`,
    														`recipie`.`default_amount`,
    														`production`.`name`,
    														`recipie`.`price`,
    														IF(`recipie`.`visible` = 1, 'checked', NULL) AS `visible`
												FROM		`recipie`
												LEFT JOIN	`production` ON `recipie`.`ingredient_id` = `production`.`id`
												WHERE 		`recipie`.`id` = $prod_id"));
	return $res;
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>		    
	    	<legend>ძირითადი ინფორმაცია</legend>
    		<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="rec_production">პროდუქტი</label></td>
					<td>
						<div class="seoy-row" id="production_name_seoy">
							<input type="text" id="rec_production" class="idle seoy-address" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['name'] . '" />
							<button class="combobox" id="combo">rec_production</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="rec_quantity">რაოდენობა</label></td>
					<td>
						<input type="text" id="rec_quantity" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['quantity'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="rec_quantity">ფასი</label></td>
					<td>
						<input type="text" id="rec_price" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['price'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="default_amount">ნაგულისხმევი რაოდ.</label></td>
					<td>
						<input type="number" id="default_amount" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['default_amount'] . '" min="0" />
					</td>
				</tr>				
				<tr>
					<td style="width: 100px;"><label for="category">რეცეპტის მენიუში</label></td>
					<td>
						<input type="checkbox" name="visible" class="check" id="checkbox" '. $res['visible'] .'/>
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="production_id" value="' . $res['id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}

?>